package com.kreateinfinity.msdi.web.authentication;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.kreateinfinity.msdi.model.authentication.User;
import com.kreateinfinity.msdi.service.authentication.UserAuthenticationService;
@Controller
public class PostLoginController {


	@Autowired
	org.springframework.web.servlet.view.json.JsonView jsonView;
	private static Logger LOGGER = Logger.getLogger(PostLoginController.class);
	@Autowired
	private UserAuthenticationService userAuthenticationService;
	@RequestMapping(value="/home.html")
	public ModelAndView homeSubmit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		LOGGER.debug("is invoked");

		
		request.setAttribute("headerTab", "basic");
		ModelAndView modelAndView = new ModelAndView("home");
		return modelAndView;
		
	}
	@RequestMapping(value = "afterlogin.html", method=RequestMethod.POST)
	public ModelAndView getAfterlogin(Model model,HttpServletRequest request,HttpServletResponse response) throws Exception  {
		String userid = request.getParameter("username");
		String pwd = request.getParameter("password");
		System.out.println("userid :" +userid);
		System.out.println("pwd :" +pwd);
		User user = userAuthenticationService.getLogin(userid,pwd);
		request.getSession().setAttribute("user", user);
		Map<String,Object> map = new LinkedHashMap<String,Object>();
		System.out.println(user);
		if (user == null) {

			

			map.put("errors", "Invalid Username/Password");
			
			
		}else{
			map.put("success", true);
			
			
		}
		return new ModelAndView(this.jsonView, map);

	}

}
